package com.subang.function.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.subang.common.core.domain.entity.SysUser;
import com.subang.common.utils.SecurityUtils;
import com.subang.function.domain.SbExamReview;
import com.subang.function.service.ISbExamReviewService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.subang.common.annotation.Log;
import com.subang.common.core.controller.BaseController;
import com.subang.common.core.domain.AjaxResult;
import com.subang.common.enums.BusinessType;
import com.subang.function.domain.SysUserCollect;
import com.subang.function.service.ISysUserCollectService;
import com.subang.common.utils.poi.ExcelUtil;
import com.subang.common.core.page.TableDataInfo;

/**
 * 用户收藏Controller
 * 
 * @author subei
 * @date 2025-03-22
 */
@RestController
@RequestMapping("/function/collect")
public class SysUserCollectController extends BaseController
{
    @Autowired
    private ISysUserCollectService sysUserCollectService;

    @Autowired
    private ISbExamReviewService sbExamReviewService;

    /**
     * 查询用户收藏列表
     */
    @PreAuthorize("@ss.hasPermi('function:collect:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserCollect sysUserCollect)
    {
        startPage();
        List<SysUserCollect> list = sysUserCollectService.selectSysUserCollectList(sysUserCollect);
        return getDataTable(list);
    }

    /**
     * 导出用户收藏列表
     */
    @PreAuthorize("@ss.hasPermi('function:collect:export')")
    @Log(title = "用户收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserCollect sysUserCollect)
    {
        List<SysUserCollect> list = sysUserCollectService.selectSysUserCollectList(sysUserCollect);
        ExcelUtil<SysUserCollect> util = new ExcelUtil<SysUserCollect>(SysUserCollect.class);
        util.exportExcel(response, list, "用户收藏数据");
    }

    /**
     * 获取用户收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('function:collect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysUserCollectService.selectSysUserCollectById(id));
    }

    /**
     * 新增用户收藏
     */
    @Log(title = "用户收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserCollect sysUserCollect)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sysUserCollect.setUserId(user.getUserId());
        List<SysUserCollect> list = sysUserCollectService.selectSysUserCollectList(sysUserCollect);

        if(sysUserCollect.getIsCollected()==1){
            if (!list.isEmpty()) {
                return AjaxResult.error("用户已收藏");
            }
            int result = sysUserCollectService.insertSysUserCollect(sysUserCollect);
            if(result > 0 && "模考复盘".equals(sysUserCollect.getCollectType())){
                SbExamReview sbExamReview = sbExamReviewService.selectSbExamReviewById(sysUserCollect.getCollectId());
                sbExamReview.setLikes(sbExamReview.getLikes()+1);
                sbExamReviewService.updateSbExamReview(sbExamReview);
            }
            return toAjax(result);
        }else{
            SysUserCollect sysUserCollect1 = list.get(0);

            int result = sysUserCollectService.deleteSysUserCollectById(sysUserCollect1.getId());
            if(result > 0 && "模考复盘".equals(sysUserCollect.getCollectType())){
                SbExamReview sbExamReview = sbExamReviewService.selectSbExamReviewById(sysUserCollect.getCollectId());
                sbExamReview.setLikes(sbExamReview.getLikes()-1);
                sbExamReviewService.updateSbExamReview(sbExamReview);
            }
            return toAjax(result);
        }
    }

    /**
     * 修改用户收藏
     */
    @PreAuthorize("@ss.hasPermi('function:collect:edit')")
    @Log(title = "用户收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserCollect sysUserCollect)
    {
        return toAjax(sysUserCollectService.updateSysUserCollect(sysUserCollect));
    }

    /**
     * 删除用户收藏
     */
    @PreAuthorize("@ss.hasPermi('function:collect:remove')")
    @Log(title = "用户收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysUserCollectService.deleteSysUserCollectByIds(ids));
    }
}
